package com.example.huizhou.controller;

import com.example.huizhou.Service.AdminService;
import com.example.huizhou.Service.StudentService;
import com.example.huizhou.Service.TeacherService;
import com.example.huizhou.bean.Student;
import com.example.huizhou.bean.Teacher;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminCotroller {
    @Resource
    AdminService adminService;
    @Resource
    TeacherService teacherService;
    @Resource
    StudentService studentService;

    @RequestMapping("/login")
    public String login(){
        return "register";
    }
    @RequestMapping("/mlogin")
    @ResponseBody
    public String mlogin(String ID, String psw){
        if(ID==null){
            return "id";
        }else if(psw==null){
            return "psw";
        }else{
            String password = adminService.findadmin(ID);
            if(!psw.equals(password)||password=="") return "error";
            return "yes";
        }
    }

//    学生信息管理
//    登录
    @RequestMapping("/getAllstudent")
    public String getAllStu(Model model){
        List<Student> stu = studentService.findAll();
        model.addAttribute("student",stu);
        return "admin_stu";
    }

    //添加学生信息
    @RequestMapping("/addstu")
    @ResponseBody
    public String addstu(int id,String stuid,String name,String sex,String psw,String faculty,String major,String stuclass,String year){
        if(stuid.equals("")||name.equals("")||sex.equals("")||faculty.equals("")||major.equals("")||stuclass.equals("")||year.equals("")){
            return "error";
        }
        psw="123456";
        String psw1 = studentService.findsd(stuid);
        if(psw1==null) {
            studentService.add(id,stuid,name,psw,sex,major,stuclass,year,faculty);
            return "yes";
        }
        return "error";
    }

    //删除学生信息
    @RequestMapping("/delstu")
    public String delstu(int id){
        studentService.delete(id);
        return "redirect:/admin/getAllstudent";
    }



    //教师信息管理
    @RequestMapping("/getAllteacher")
    public String getAllUser(Model model){
        List<Teacher> list = teacherService.findAll();
        model.addAttribute("teacher",list);
        return "admin";
    }

    @RequestMapping("/del")
    public String deltc(int id){
        System.out.println(id);
        teacherService.delete(id);

        return "redirect:/admin/getAllteacher";
    }

    @RequestMapping("/add")
    @ResponseBody
    public String addtc(int id,String name,String sex,String faculty,String password){
//        System.out.println("id: "+id+" name: "+name+" "+"sex: "+sex+" "+"faculty: "+faculty+"password: "+password);
        password="123456";
        //检查id合法性
        String name1=teacherService.findtc(id);
        if(name==""||sex==""||faculty=="") return "error";
        if(name1==null){
            teacherService.add(id, name, password, sex, faculty);
            return "yes";
        }else {
            return "error";
        }
    }

}
